<?php
/**
 * 描述：邮件发送类。
 */
class Email implements IConfig_Set
{
    public $Server=NULL;
    public $Server_Port=25;
    public $Username=NULL;
    public $Password=NULL;
    public $IsHTMLEmail=TRUE;

    public $Title=NULL;    
    public $Content=NULL;
    public $Recipients=NULL;

    public $IsSuccess=FALSE;
    public $Reason=NULL;

    public function __Construct($smtpServer=NULL,$username=NULL,$password=NULL)
    {
        #是否配置
        $isConfig=TRUE;
        if($smtpServer==NULL||$username==NULL||$password==NULL){
            $isConfig=TRUE;
        }

        #如果有一个没配置都配置一下
        if($isConfig){
            $this->SetConfig();
        }
    }

    public function Send()
    {
        #打开链接
        $socket=@fsockopen($this->Server,$this->Server_Port);
        if($socket)
        {           
            stream_set_blocking($socket,TRUE);
            $result=fgets($socket);
            
            #尝试连接
            fputs($socket,"HELO shipinall.cn \r\n");
            $result=fgets($socket);
            //echo $result.'<br/>';

            #有待调查
            fputs($socket,"AUTH LOGIN \r\n");
            $result=fgets($socket);
            //echo $result.'<br/>';
            fputs($socket,base64_encode($this->Username)."\r\n");
            $result=fgets($socket);
            //echo $result.'-username<br/>';
            fputs($socket,base64_encode($this->Password)."\r\n");
            $result=fgets($socket);
            //echo $result.'-password<br/>';
            fputs($socket,"MAIL FROM:<$this->Username>\r\n");
            $result=fgets($socket);
            //echo $result.'<br/>';
            fputs($socket,"RCPT TO: <$this->Recipients>\r\n");
            $result=fgets($socket);
            //echo $result.'<br/>';
            fputs($socket,"DATA\r\n");
            $result=fgets($socket);
            //echo $result.'<br/>';

            #HTML邮件
            if($this->IsHTMLEmail){
                fputs($socket,'Content-type:text/html;'.'\r\n');
            }
            else{
                fputs($socket,'Content-type:text/plain;'.'\r\n');
            }

            #组织邮件内容
            fputs($socket,"From: 中国船舶配套网 <$this->Username>\r\n");
            fputs($socket,"Subject: $this->Title\r\n");
            fputs($socket,"To:$this->Recipients\r\n");
            fputs($socket,"\r\n");
            fputs($socket,$this->Content."\r\n");
            fputs($socket,"."."\r\n");
            fputs($socket,"QUIT\r\n");
            $result=fgets($socket);
            //echo $result.'<br/>';           
            fclose($socket);

//            fputs($socket,"Content-Type: text/plain;\r\n");
//            
//            fputs($socket,"Content-Transfer-Encoding: base64\r\n\r\n");
        }
        else
        {
            return $this->Reason='服务器链接超时';
        }
    }

    /**
     * 描述：读取默认的Config配置。
     */
    public function SetConfig()
    {
        if(class_exists('Config')&&method_exists('Config','Email'))
        {
            foreach(Config::Email() as $option=>$value)
            {
                if($this->$option==NULL){   //如果配置项没有就读默认配置
                    $this->$option=$value;
                }
            }
        }
    }
}
?>